﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using Entity;

namespace SalesVMT
{
    public partial class frmCalculationUnit : Form
    {
        DialogResult dlgResult;
        CalculationUnitInfo CalculationUnitinf;
        public frmCalculationUnit(int _CalculationUnit_ID)
        {
            InitializeComponent();
            CalculationUnitinf = new CalculationUnitInfo();
            CalculationUnitinf.CalculationUnit_ID = _CalculationUnit_ID;
            if (_CalculationUnit_ID > 0)
            {
                CalculationUnitinf = CalculationUnitDB.GetInfo(_CalculationUnit_ID);
                
                InfoToForm();
            }
            
            this.KeyPreview = true;
            dlgResult = DialogResult.Cancel;
        }
        private void FormToInfo()
        {
            CalculationUnitinf.CalculationUnit_Name = txtName.Text;
        }
        private void InfoToForm()
        {
            txtName.Text = CalculationUnitinf.CalculationUnit_Name;

        }
        private bool CheckValid()
        {
            if (txtName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Tên đơn vị trống! Hãy nhập lại.", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtName.Focus();
                return false;
            }
            return true;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if(!CheckValid()) return;
            FormToInfo();
            if (CalculationUnitinf.CalculationUnit_ID > 0)//Update
            {
                if (CalculationUnitDB.Exist(CalculationUnitinf.CalculationUnit_ID, txtName.Text))
                {
                    MessageBox.Show(Message.Exist, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (CalculationUnitDB.Update(CalculationUnitinf))
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Update_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(Message.Update_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else //Create
            {
                if (CalculationUnitDB.Exist(CalculationUnitinf.CalculationUnit_ID, txtName.Text))
                {
                    MessageBox.Show(Message.Exist, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                CalculationUnitinf.CalculationUnit_ID = CalculationUnitDB.Insert(CalculationUnitinf);
                if (CalculationUnitinf.CalculationUnit_ID > 0)
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Create_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(Message.Create_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmCalculationUnit_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            CalculationUnitinf.CalculationUnit_ID = 0;
        }

        private void frmCalculationUnit_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F2:
                    btnSaveToNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Escape:
                    
                    this.DialogResult = dlgResult;
                    this.Close();
                    break;
            }
        }

        private void btnSaveToNew_Click(object sender, EventArgs e)
        {
            btnSave_Click(null, null);
            btnNew_Click(null,null);

        }

    }
}